﻿// See https://aka.ms/new-console-template for more information
using FilterIllegalCharacter.Core;
using Flurl.Http;
using System;
using System.Diagnostics.Metrics;
using System.Drawing;
using System.Linq;
using Xceed.Document.NET;
using Xceed.Words.NET;
using static System.Net.Mime.MediaTypeNames;

string url = "https://gitee.com/lddxdmy/keywords/raw/master/illegal-character.txt";
var illegalCharacter = await url.GetStringAsync();
var words1 = illegalCharacter.Split("\r\n");
var words2 = illegalCharacter.Split("\r\n");
words1.Concat(words2);
var words = words1.Distinct().ToArray();

string text = null;
FilterHelper.Instance.InitDictionary(words);

string filePath = "C:\\Users\\Administrator\\Desktop\\直播脚本测试版本1(有表格)(1).docx";
using (var document = DocX.Load(filePath))
{
    var rNodeList = FilterHelper.Instance.GetRNodeList(new CancellationTokenSource().Token, string.Join("", document.Paragraphs.Select(item => item.Text)));
    var hitCount = rNodeList.Select(item => item.text).GroupBy(s => s)
                           .Select(g => new { Word = g.Key, Count = g.Count() })
                           .OrderByDescending(s => s.Count)
                           .ThenBy(s => s.Word);
    foreach (var item in hitCount)
    {
        Console.WriteLine($"{item.Word}x{item.Count}");
    }
    //根据索引和长度高亮文本
    document.HighlightText(rNodeList, Highlight.red);
    //保存修改后的文档
    //document.Save();
    text = document.Text;
}
//Console.WriteLine("文本高亮完成。");
//text = "安安看关键字1啊大啊访问f啊啊是否关键字2啊打发123是发生大为发关键字4青蛙2141 而无法关键字3啊撒发射点3221！@#1312432··士大夫十分开袋即食按时发放232啊书法大赛关注主播阿斯顿我去二分123！@#￥@#";
//var filter1 = FilterHelper.Instance.FilterWithChar(text, '*');
//var filter2 = FilterHelper.Instance.FilterWithStr(text, "*");
Console.WriteLine("转换完成");